package com.huiminpay.merchant.service;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.merchant.api.IStaffService;
import com.huiminpay.merchant.covert.StaffCovert;
import com.huiminpay.merchant.dto.StaffDTO;
import com.huiminpay.merchant.entity.Staff;
import com.huiminpay.merchant.mapper.StaffMapper;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


@Service
public class IStaffServiceImpl implements IStaffService {

    @Autowired
    StaffMapper staffMapper;


    @Override
    public void createStaff(StaffDTO staffDTO) {
        
        //非空校验
        if (staffDTO.getFullName() == null || "".equals(staffDTO.getFullName())){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        if (staffDTO.getUsername() == null || "".equals(staffDTO.getUsername())){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        if (staffDTO.getMobile() == null || "".equals(staffDTO.getMobile())){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        if (staffDTO.getMerchantId() == null){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        if (staffDTO.getStoreId() == null){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        if (staffDTO.getPosition() == null || "".equals(staffDTO.getPosition())){
            throw new BusinessException(CommonErrorCode.E_200202);
        }
        //校验同一个商户下   用户名和电话是否唯一

        Integer i =  staffMapper.queryUsernameAndMobile(
                staffDTO.getMerchantId(),staffDTO.getUsername(),staffDTO.getMobile());

        if (i > 0){
            throw new BusinessException(CommonErrorCode.E_200203);
        }

        //插入数据 格式化状态
        staffDTO.setStaffStatus(true);
        Staff staff = StaffCovert.INSTANCE.dto2Entity(staffDTO);
        staffMapper.insert(staff);
        //在员工和商店表进行绑定 TODO
    }

    @Override
    public void updateStaff(StaffDTO staffDTO) {
        //商户Id 商店ID 姓名不能更改不能更改
        staffDTO.setMerchantId(null);
        staffDTO.setFullName(null);
        staffDTO.setStoreId(null);
        Staff staff = StaffCovert.INSTANCE.dto2Entity(staffDTO);
        staffMapper.updateById(staff);
    }

    @Override
    public void deleteStaff(Long staffId) {

        staffMapper.deleteById(staffId);
    }
}
